/**
 * This activity represents file-save dialog
 */
package org.amse.ys.sndeditor.ui;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;

import org.amse.ys.sndeditor.model.Localiser;
import org.amse.ys.sndeditor.model.drawer.AndroidDrawer;
import org.amse.ys.sndeditor.model.drawer.RequestString;
import org.amse.ys.sndeditor.model.filesearcher.FileSearcher;
import org.amse.ys.sndeditor.ui.R.string;

import android.app.*;
import android.view.*;
import android.view.View.OnKeyListener;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SoundSaveDialog extends Activity {

	private final ArrayList<String> myFileNameList = new ArrayList<String>();
	private final ArrayList<File> myFileList = new ArrayList<File>();
	private ArrayAdapter<String> myAa;
	private EditText myEditText;
	private final LinkedList<File> myFolderHierarcy = new LinkedList<File>();

	public void fileReady(String name) {
		((RequestString) (AndroidDrawer.myActionToDo)).run(name);
		finish();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, Menu.FIRST, 0, Localiser.getString(string.cancel));
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == Menu.FIRST) {
			fileReady("");
			return true;
		}
		return false;
	}

	private void tryToOpen(String fileName) {
		int i = myFileNameList.indexOf(fileName);
		if (i >= 0) {
			tryToOpen(i);
		} else {
			System.out.println(myFolderHierarcy.getLast() + "/" + fileName);
			fileReady(myFolderHierarcy.getLast() + "/" + fileName);

		}
	}

	private void tryToOpen(int index) {
		if (myFileList.get(index).isDirectory()) {
			if ((index == 0) && (myFolderHierarcy.size() > 1)) {
				myFolderHierarcy.removeLast();
				myFolderHierarcy.removeLast();
			}
			fillWithFiles(myFileList.get(index));
		} else {
			fileReady(myFileList.get(index).getName());
			System.out.println(myFileList.get(index).getName());
		}
	}

	private void fillWithFiles(File folder) {
		myFileList.clear();
		myFileNameList.clear();

		if (!myFolderHierarcy.isEmpty()) {
			myFileList.add(myFolderHierarcy.getLast());
			myFileNameList.add("..");
		}

		myFolderHierarcy.add(folder);
		for (File o : folder.listFiles()) {
			if (o.isDirectory()) {
				myFileList.add(o);
				myFileNameList.add(o.getName() + "/");
			}
		}
		for (File o : folder.listFiles()) {
			if ((!o.isDirectory() && (FileSearcher.supportedFile(o)))) {
				myFileList.add(o);
				myFileNameList.add(o.getName());
			}
		}
		myAa.notifyDataSetChanged();
	}

	@Override
	public void onCreate(Bundle icicle) {
		// init activity, set listeners
		System.out.println("1");
		super.onCreate(icicle);
		System.out.println("1");
		setContentView(R.layout.file_save_dialog);
		System.out.println("1");

		ListView myListView = (ListView) findViewById(R.id.mySaveListView);
		System.out.println("1");
		myEditText = (EditText) findViewById(R.id.mySaveEditText);
		System.out.println("1");
		myAa = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, myFileNameList);
		System.out.println("1");
		myListView.setAdapter(myAa);

		System.out.println("1");
		myEditText.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (event.getAction() == KeyEvent.ACTION_DOWN) {
					if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
							|| (keyCode == KeyEvent.KEYCODE_ENTER)) {
						tryToOpen(myEditText.getText().toString());

						// fileReady(myEditText.getText().toString());
						return true;
					}
				}
				return false;
			}
		});
		System.out.println("1");

		myListView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				tryToOpen(arg2);
			}
		});

		System.out.println("1");
		// default folder
		fillWithFiles(new File("/sdcard"));
		myAa.notifyDataSetChanged();

	}

}
